package com.thinkfit.jy.dialog;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import androidx.constraintlayout.widget.ConstraintLayout;

import com.common.lib.constant.EventBusEvent;
import com.jaygoo.widget.OnRangeChangedListener;
import com.jaygoo.widget.RangeSeekBar;
import com.thinkfit.jy.R;
import com.thinkfit.jy.bean.CreateTrainItemBean;
import com.thinkfit.jy.utils.BluetoothOrder;
import com.thinkfit.jy.utils.SerialPortUtils;
import com.thinkfit.jy.utils.ToastUtils;
import com.thinkfit.jy.utils.Utils;

import org.greenrobot.eventbus.EventBus;

import java.util.HashMap;

public class CreateTrainPopupWindow extends PopupWindow implements View.OnClickListener , PopupWindow.OnDismissListener {

    private Context mContext;

    private View view,v1;
    ImageView ivTimeReduce,ivTimeAdd,ivSpeedReduce,ivSpeedAdd,ivSlopReduce,ivSlopAdd;
    TextView tvTimeNum,tvSpeedNum,tvSlopNum,tvOk;
    RangeSeekBar rsbTime,rsbSpeed,rsbSlop;
    ConstraintLayout clBg;

    int time,speed,slop,maxSpeed,maxSlop;
    CreateTrainItemBean bean;


    public CreateTrainPopupWindow(Context context, CreateTrainItemBean info){
        mContext = context;
        bean = info;
        view = LayoutInflater.from(context).inflate(R.layout.popupwindow_create_train, null);

        clBg = view.findViewById(R.id.cl_bg);
        v1 = view.findViewById(R.id.v1);

        ivTimeReduce = view.findViewById(R.id.iv_time_reduce);
        ivTimeAdd = view.findViewById(R.id.iv_time_add);
        tvTimeNum = view.findViewById(R.id.tv_time_num);
        rsbTime = view.findViewById(R.id.rsb_time);

        ivSpeedReduce = view.findViewById(R.id.iv_speed_reduce);
        ivSpeedAdd = view.findViewById(R.id.iv_speed_add);
        tvSpeedNum = view.findViewById(R.id.tv_speed_num);
        rsbSpeed = view.findViewById(R.id.rsb_speed);

        ivSlopReduce = view.findViewById(R.id.iv_slop_reduce);
        ivSlopAdd = view.findViewById(R.id.iv_slop_add);
        tvSlopNum = view.findViewById(R.id.tv_slop_num);
        rsbSlop = view.findViewById(R.id.rsb_slop);

        tvOk = view.findViewById(R.id.tv_ok);

        rsbTime.setRange(0,100);
        if(bean.getMaxSpeed() <= 0){
            rsbSpeed.setRange(0,30);
            maxSpeed = 30;
        }else {
            rsbSpeed.setRange(0,bean.getMaxSpeed());
            maxSpeed = bean.getMaxSpeed();
        }

        if(bean.getMaxSlop() <= 0){
            rsbSlop.setRange(0,18);
            maxSlop = 18;
        }else {
            rsbSlop.setRange(0,bean.getMaxSlop());
            maxSlop = bean.getMaxSlop();
        }
        if(!TextUtils.isEmpty(bean.getTime())){
            rsbTime.setProgress(Float.valueOf(bean.getTime()));
            tvTimeNum.setText(bean.getTime());
            time = Integer.valueOf(bean.getTime());
        }
        if(!TextUtils.isEmpty(bean.getSpeed())){
            rsbSpeed.setProgress(Float.valueOf(bean.getSpeed()));
            speed = Integer.valueOf(bean.getSpeed());
            tvSpeedNum.setText(Utils.getNewFloat(speed/10f));

        }
        if(!TextUtils.isEmpty(bean.getSlop())){
            rsbSlop.setProgress(Float.valueOf(bean.getSlop()));
            tvSlopNum.setText(String.valueOf(bean.getSlop()));
            slop = Integer.valueOf(bean.getSlop());
        }

        rsbTime.setOnRangeChangedListener(new OnRangeChangedListener() {
            @Override
            public void onRangeChanged(RangeSeekBar view, float leftValue, float rightValue, boolean isFromUser) {
                tvTimeNum.setText(String.valueOf((int)leftValue));
                time = (int)leftValue;
            }

            @Override
            public void onStartTrackingTouch(RangeSeekBar view, boolean isLeft) {

            }

            @Override
            public void onStopTrackingTouch(RangeSeekBar view, boolean isLeft) {

            }
        });
        rsbSpeed.setOnRangeChangedListener(new OnRangeChangedListener() {
            @Override
            public void onRangeChanged(RangeSeekBar view, float leftValue, float rightValue, boolean isFromUser) {
                tvSpeedNum.setText(Utils.getNewFloat(leftValue/10f));
                speed = (int)leftValue;
            }

            @Override
            public void onStartTrackingTouch(RangeSeekBar view, boolean isLeft) {

            }

            @Override
            public void onStopTrackingTouch(RangeSeekBar view, boolean isLeft) {

            }
        });
        rsbSlop.setOnRangeChangedListener(new OnRangeChangedListener() {
            @Override
            public void onRangeChanged(RangeSeekBar view, float leftValue, float rightValue, boolean isFromUser) {
                tvSlopNum.setText(String.valueOf((int)leftValue));
                slop = (int)leftValue;
            }

            @Override
            public void onStartTrackingTouch(RangeSeekBar view, boolean isLeft) {

            }

            @Override
            public void onStopTrackingTouch(RangeSeekBar view, boolean isLeft) {

            }
        });




        ivTimeReduce.setOnClickListener(this);
        ivTimeAdd.setOnClickListener(this);
        ivSpeedReduce.setOnClickListener(this);
        ivSpeedAdd.setOnClickListener(this);
        ivSlopReduce.setOnClickListener(this);
        ivSlopAdd.setOnClickListener(this);
        tvOk.setOnClickListener(this);
        v1.setOnClickListener(this);
        clBg.setOnClickListener(this);


        this.setContentView(view);
        WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        int width = wm.getDefaultDisplay().getWidth();

        // 设置SelectPicPopupWindow弹出窗体的宽
        this.setWidth(LinearLayout.LayoutParams.MATCH_PARENT);
        // 设置SelectPicPopupWindow弹出窗体的高
        this.setHeight(LinearLayout.LayoutParams.MATCH_PARENT);
        // 设置SelectPicPopupWindow弹出窗体可点击
        this.setFocusable(true);
        // 实例化一个ColorDrawable颜色为半透明
        ColorDrawable dw = new ColorDrawable(Color.parseColor("#4D000000"));
//        ColorDrawable dw = new ColorDrawable(Color.parseColor("#ffffff"));
        // 设置SelectPicPopupWindow弹出窗体的背景
        this.setBackgroundDrawable(dw);
        this.setOnDismissListener(this);

    }

    public void show(Activity context) {
        showAtLocation(context.getWindow().getDecorView(), Gravity.CENTER, 0, 0);
    }



    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.iv_time_reduce:
                time --;
                if(time <= 0){
                    time = 0;
                }
                rsbTime.setProgress(time);
                break;
            case R.id.iv_time_add:
                time ++;
                if(time >= 100){
                    time = 100;
                }
                rsbTime.setProgress(time);
                break;
            case R.id.iv_speed_reduce:
                speed --;
                if(speed <= 0){
                    speed = 0;
                }
                rsbSpeed.setProgress(speed);
                break;
            case R.id.iv_speed_add:
                speed ++;
                if(speed >= maxSpeed){
                    speed = maxSpeed;
                }
                rsbSpeed.setProgress(speed);
                break;
            case R.id.iv_slop_reduce:
                slop --;
                if(slop <= 0){
                    slop = 0;
                }
                rsbSlop.setProgress(slop);
                break;
            case R.id.iv_slop_add:
                slop ++;
                if(slop >= maxSlop){
                    slop = maxSlop;
                }
                rsbSlop.setProgress(slop);
                break;
            case R.id.tv_ok:
                if(time <= 0 || speed <= 0){
                    ToastUtils.getInstance().show(mContext,mContext.getString(R.string.app_train_add_section_hint));
                    return;
                }
                HashMap<String, Object> map = new HashMap<>();
                bean.setSlop(slop+"");
                bean.setTime(time+"");
                bean.setSpeed(speed+"");
                map.put(EventBusEvent.CREATE_TRAIN_INFO,bean);
                EventBus.getDefault().post(map);
                dismiss();
                break;
            case R.id.cl_bg:
                dismiss();
                 break;
        }
    }

    @Override
    public void onDismiss() {

    }
}
